package com.vdf.sisconsolid.rest.requestresponsedata;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

public class RequestAndRespondHandler {

	public static Map<String, Object> getResponseRest(Boolean success,
			String dataType, String message, Object data) {
		return getResponseRest(success, dataType, message, data, null);
	}
	
	public static Map<String, Object> getResponseRest(Boolean success,
			String dataType, String message) {
		return getResponseRest(success, dataType, message, null, null);
	}
	
	public static Map<String, Object> getResponseRest(Boolean success,
			String dataType, String message, Object data, Integer totalCount) {
		Map<String, Object> restResponset = new HashMap<String, Object>();
		restResponset.put(ReqRespData.success.toString(), success);
		restResponset.put(ReqRespData.type.toString(), dataType);
		restResponset.put(ReqRespData.message.toString(), message);
		restResponset.put(ReqRespData.data.toString(), data);
		if (totalCount != null) {
			restResponset.put(ReqRespData.count.toString(), totalCount);
		}
		return restResponset;
	}

	@SuppressWarnings("rawtypes")
	public static Object fromJson(String json, Class clazz) {
		if (json == null) {
			return null;
		}
		Object object = null;
		try {
			object = new ObjectMapper()
					.readValue(json, FilterParameter[].class);
		} catch (JsonParseException e) {
		} catch (JsonMappingException e) {
		} catch (IOException e) {
		}
		return object;
	}
}
